<?php

namespace common\models\member;

use Yii;
use common\models\user\UserConnect;

/**
 * This is the model class for table "{{%member_cashout}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $member_id 会员ID
 * @property int $account_id 会员账户ID
 * @property int $value 结算金额
 * @property int $recharge 手续费
 * @property int $ip IP
 * @property int $status 审核状态
 * @property string $mark 备注
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 * @property MemberCashoutOrder $order 提现订单
 * @property MemberAccount $account 佣金账户
 */
class MemberCashout extends \common\db\ActiveRecord {

    use \common\traits\Constants;
    use \common\traits\relation\UserData;

    //审核状态
    CONST STATUS_REFUSE = -10; //拒绝
    CONST STATUS_CANCEL = -1; //取消
    CONST STATUS_DEFAULT = 0; //等待审核
    CONST STATUS_AUDITING = 1; //审核中
    CONST STATUS_ACTIVE = 10; //审核通过
    CONST STATUS_SUCCESS = 100; //发放成功

    /**
     * {@inheritdoc}
     */

    public static function tableName() {
        return '{{%member_cashout}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'member_id', 'account_id', 'value', 'recharge', 'ip', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['mark'], 'string', 'max' => 255],
            [['openid'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'member_id' => '会员ID',
            'account_id' => '会员账户ID',
            'value' => '申请结算金额',
            'recharge' => '手续费',
            'openid' => 'OPENID',
            'ip' => 'IP',
            'status' => '审核状态',
            'mark' => '备注',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
            'user' => '用户',
        ];
    }

    public function getMember() {
        return $this->hasOne(Member::class, ['id' => 'member_id']);
    }

    public function getAccount() {
        return $this->hasOne(MemberAccount::class, ['id' => 'member_id']);
    }

    public function getOrder() {
        return $this->hasOne(MemberCashoutOrder::class, ['cashout_id' => 'id']);
    }

    public function getConnect() {
        return $this->hasOne(UserConnect::class, ['user_id' => 'user_id'])->andWhere([
            'client' => UserConnect::CLIENT_WECHAT_ACCOUNT
        ]);
    }

    public function getLogs() {
        return $this->hasMany(MemberCashoutLog::class, ['cashout_id' => 'id']);
    }

    public function getConsoles() {
        $consoles = [];
        if ($this->status == static::STATUS_REFUSE || $this->status == static::STATUS_ACTIVE) {
            $consoles[] = [
                'label' => '重置审核',
                'url' => ['reset', 'id' => $this->id],
                'options' => [
                    'data-confirm' => '确认重置当前佣金提现申请审核吗?',
                    'data-method' => 'POST'
                ]
            ];
            if ($this->status == static::STATUS_ACTIVE && $this->order && !$this->order->isPaid) {
                $consoles[] = [
                    'label' => '微信企业付款',
                    'url' => ['cross', 'id' => $this->id],
                    'options' => [
                        'class' => 'btn btn-sm btn-success',
                        'data-confirm' => '系统即将付款给佣金账户对应的微信账号，确认后将执行自动汇款。确认继续？',
                        'data-method' => 'POST',
                    ]
                ];
            }
        } elseif ($this->status == static::STATUS_DEFAULT) {
            $consoles[] = [
                'label' => '开始审核',
                'url' => ['accept', 'id' => $this->id],
                'options' => [
                    'data-method' => 'POST',
                ]
            ];
        } elseif ($this->status == static::STATUS_AUDITING) {
            $consoles[] = [
                'label' => '<i class="ionicons ion-android-checkmark-circle"></i> 通过',
                'url' => ['allow', 'id' => $this->id],
                'options' => [
                    'data-confirm' => '确认通过该提现申请?',
                    'data-method' => 'POST'
                ]
            ];
            $consoles[] = [
                'label' => '<i class="ionicons ion-android-cancel"></i> 驳回',
                'url' => ['refuse', 'id' => $this->id],
                'tag' => 'ajaxModal'
            ];
        }

        return $consoles;
    }

}
